package com.godman.liu.loggingdemo.filter;

import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "cross", urlPatterns = "/*")
@Order(21)
public class CrossFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

//        System.out.println("requestUri:"+httpServletRequest.getRequestURI());
//        System.out.println("queryString:"+httpServletRequest.getQueryString());
//        System.out.println("contextPath:"+httpServletRequest.getContextPath());
//        System.out.println("remoteAddress:"+httpServletRequest.getRemoteAddr());
//        System.out.println("remoteHost:"+httpServletRequest.getRemoteHost());
//        System.out.println("remoteport:"+httpServletRequest.getRemotePort());
//        System.out.println("servletPath:"+httpServletRequest.getServletPath());

        // 转发，将请求转发到/qqai/上，体现了过滤器可修改参数、请求等,参数说相对路径
//        httpServletRequest.getRequestDispatcher("/").forward(httpServletRequest,httpServletResponse);

        // 重定向，防止过滤器里不停重定向
//        if(httpServletRequest.getRequestURI().contains("name")) {
//            httpServletResponse.sendRedirect("/qqai/");
//        }

//        String path = httpServletRequest.getRequestURI().substring(httpServletRequest.getContextPath().length()).replaceAll("[/]+$", "");
//        System.out.println(path);

        /**
         * 因为filter的加载顺序在spring之前，导致@value拿不到yml文件里的属性值，只能通过静态文件的方式
         */
//        ServletContext context = httpServletRequest.getServletContext();
//        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
//        String iplistStr = ctx.getEnvironment().getProperty("state.vivi");
//        String isnow = ctx.getEnvironment().getProperty("state.isnow");
//
//        System.out.println("CrossFilter,vivi:" + iplistStr);
//        System.out.println("CrossFilter,isnow:" + isnow);


        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");
        httpServletResponse.setHeader("Access-Control-Max-Age", "3600");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");

        if (httpServletRequest.getMethod().equals("OPTIONS")) {
            httpServletResponse.setStatus(HttpServletResponse.SC_OK);
            return;
        }

        filterChain.doFilter(servletRequest, servletResponse);

    }

    @Override
    public void destroy() {

    }
}
